package com.Entity.Product;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import com.DAO.Product_keyName;
import com.DAO.Product_keyValue;
import com.Entity.Basic;

public class Product extends Basic implements ProductImplement {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Product.java
	 * @Package com.Entity.Product
	 * @author ufo
	 */

	private Map<String, String[]> keyValueMap = new HashMap<String, String[]>();
	// keyValueMap<product_keyNameId,String[]{product_keyValue,product_keyValueId}>
	private Product_descriptionImplement description = null;
	private String productId = null;

	public static List iteratorProducts(int max, int first,
			String productOwnerId) {
		List list = databaseControler.hibernateSess
				.createQuery(
						"select productId from Product_keyValue where product_keyNameId=:product_keyNameId and product_keyValue = :productOwnerId")
				.setString("product_keyNameId", PRODUCT_KEYNAME_OWNER)
				.setString("productOwnerId", productOwnerId).setMaxResults(max)
				.setFirstResult(first).list();
		return list;
	}

	public static Integer countProducts(String productOwnerId) {
		return Integer
				.parseInt(databaseControler.hibernateSess
						.createQuery(
								"select count(productId) from Product_keyValue where product_keyNameId=:product_keyNameId and product_keyValue = :productOwnerId ")
						.setString("product_keyNameId", PRODUCT_KEYNAME_OWNER)
						.setString("productOwnerId", productOwnerId).list()
						.iterator().next().toString());
	}

	public void save() {
		if (0 < keyValueMap.size()) {
			Set<String> set = keyValueMap.keySet();
			for (Iterator<String> i = set.iterator(); i.hasNext();)
				try {
					String keyName = i.next();
					List list1 = databaseControler.hibernateSess
							.createQuery(
									"select product_keyNameId from Product_keyName where product_keyName = :product_keyName")
							.setString("product_keyName", keyName).list();
					Iterator i1 = list1.iterator();
					saveKeyValue(i1.next().toString(), keyValueMap.get(keyName));
				} catch (Exception e) {
					e.printStackTrace();
				}
		}
		if (description != null)
			description.save();
	}

	public void saveKeyValue(String keyNameId, String[] target) {
		Product_keyValue keyValue = null;
		if (null == target[1] || target[1].equals("")) {
			keyValue = new Product_keyValue();
		} else {
			keyValue = (Product_keyValue) databaseControler.hibernateSess.load(
					Product_keyValue.class, target[1]);
		}
		keyValue.setProduct_keyValue(target[0]);
		keyValue.setProduct_keyNameId(keyNameId);
		keyValue.setProductId(this.productId);
		databaseControler.hibernateSess.save(keyValue);
		databaseControler.commit();
	}

	private String mapKeyName(String keyNameId) {
		Product_keyName keyName = (Product_keyName) databaseControler.load(
				Product_keyName.class, keyNameId);
		return keyName.getProduct_keyName();
	}

	public boolean map() {
		try {
			if (this.productId == null)
				return false;
			List list = databaseControler.hibernateSess
					.createQuery(
							"from Product_keyValue where productId= :productId")
					.setString("productId", this.productId).list();
			Iterator i = list.iterator();
			while (i.hasNext()) {
				Product_keyValue keyValue = (Product_keyValue) i.next();
				String[] tmp = { keyValue.getProduct_keyValue(),
						keyValue.getProduct_keyValueId() };
				keyValueMap.put(mapKeyName(keyValue.getProduct_keyNameId()),
						tmp);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public void altValue(String key, String value) {
		String[] tmp = getValueArray(key);
		tmp[0] = value;
		keyValueMap.put(key, tmp);
	}

	public void addKey(String key, String value) {
		if (this.keyValueMap == null)
			map();
		if (this.keyValueMap.containsKey(key))
			altValue(key, value);
		else {
			String[] tmp = { value, "" };
			keyValueMap.put(key, tmp);
		}
	}

	public String getValue(String key) {
		if (keyValueMap.size() <= 0)
			map();

		if (keyValueMap.get(key) != null) {
			return (keyValueMap.get(key))[0];
		} else
			return null;
	}

	public String[] getValueArray(String key) {
		if (keyValueMap.size() <= 0)
			map();
		return keyValueMap.get(key);
	}

	public Product() {
	}

	public Product(String productId) {
		setProductId(productId);
		com.DAO.Product product = (com.DAO.Product) databaseControler.load(
				com.DAO.Product.class, productId);
		setDescription(Description.load(product.getProduct_descriptionId()));
	}

	public Product(Product_descriptionImplement description) {
		com.DAO.Product product1 = new com.DAO.Product();

		product1.setProduct_descriptionId(description.getDescriptionId());
		databaseControler.save(product1);
		databaseControler.commit();

		this.productId = product1.getProductId();
		this.description = description;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		if (null == this.productId)
			this.productId = productId;
	}

	public Product_descriptionImplement getDescription() {
		if (null == productId)
			return null;
		com.DAO.Product product1 = (com.DAO.Product) databaseControler.hibernateSess
				.load(com.DAO.Product.class, productId);
		description = Description.load(product1.getProductId());
		return description;
	}

	public void setDescription(Product_descriptionImplement description) {
		this.description = description;
	}

	public Map<String, String[]> getKeyValueMap() {
		return keyValueMap;
	}

	public void setKeyValueMap(Map<String, String[]> keyValueMap) {
		this.keyValueMap = keyValueMap;
	}

	@Override
	public Map<String, String> iterator() {
		// TODO 自动生成的方法存根
		Map<String, String> result = null;
		// if (null == this.keyValueMap)
		map();
		result = new HashMap();
		Set set = this.keyValueMap.keySet();
		for (Iterator i = set.iterator(); i.hasNext();) {
			String tmp = (String) i.next();
			result.put(tmp, this.keyValueMap.get(tmp)[0]);
		}
		return result;
	}

	@Override
	public void delete() {
		// TODO 自动生成的方法存根
		com.DAO.Product product1 = (com.DAO.Product) databaseControler.hibernateSess
				.load(com.DAO.Product.class, productId);
		com.DAO.Product_keyValue keyValue = (com.DAO.Product_keyValue) databaseControler.hibernateSess
				.createQuery(
						"from Product_keyValue where productId=:productId and product_keyNameId= :product_keyNameId")
				.setString("productId", this.productId)
				.setString("product_keyNameId", PRODUCT_KEYNAME_OWNER).list()
				.iterator().next();
		databaseControler.hibernateSess.delete(product1);
		databaseControler.hibernateSess.delete(keyValue);
		databaseControler.commit();
		productId = null;
	}
}
